<?php

if( !defined("IN_ELCIUS") ) exit;

class KiwiAuth{
	
	public static $Data;
	
	//private static $PHPBB_user;
	//private static $PHPBB_auth;
	
	private static $validated;
	private static $accessLevel;
	
	/*
	0 = admin
	1 = editor
	2 = member
	3 = guest
	*/
	
	private static $PHPBB_user_data;
	
	public static function Initialize(){
		self::$Data = array();
	}
	
	// PHPBB doesn't operate very well when not in the global scope
	public static function NewPHPBBSession( &$user, &$auth ){
		self::$validated = $user->data['is_registered'];
		
		if( !self::Validated() )
		{
			// PHPBB Login Handler
			$username = request_var('username', '', true);
			$password = request_var('password', '', true);
			$result = $auth->login($username, $password);
			
			if ($result['status'] == LOGIN_SUCCESS){
				self::$validated = true;
				
			}
			else{
				// TODO: login error
			}
		}
		
		$logout = GetRequestVar('Logout');
		if( isset( $logout ) ){
			$user->session_kill();
			self::$validated = false;
		}
		
		self::$PHPBB_user_data = $user->data;
		
		self::$Data['User'] = array(
			'Name' => self::$PHPBB_user_data['username'],
			'UnreadMessages' => self::$PHPBB_user_data['user_unread_privmsg'],
			'LoggedIn' => self::Validated(),
			'IsEditor' => self::IsEditor(),
			'IsAdmin' => self::IsAdmin(),
		);
		
	}
	
	public static function GetPublicUserData(){
		return self::$Data['User'];
	}
	
	public static function Validated(){
		return self::$validated;
	}
	
	public static function IsAdmin(){ // TODO
		return self::$validated;
	}
	
	public static function IsEditor(){ // TODO
		return self::$validated or self::IsAdmin();
	}
	
	
}

?>